package com.CGFrame.struts.action.project;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.CGFrame.struts.bean.category.CateModelBean;

public class RelateShotModelAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		String projectId=request.getParameter("projectId");
		String shotModels=request.getParameter("shots");
		//shotModels形如:";modelId1:shotId1,shotId2,shotId3;modelId2:shotId4,shotId5"
		String[] group=shotModels.split(";");
		CateModelBean cmb=null;
		for(int i=0;i<group.length;i++){
			if(!group[i].equals("") && group[i]!=null){
				if(cmb==null){
					cmb=new CateModelBean(Integer.valueOf((group[i].split(":"))[0]));
					addModelToUnit((group[i].split(":"))[1], cmb);		
				}else{
					cmb.changeCateModel(Integer.valueOf((group[i].split(":"))[0]));
					addModelToUnit((group[i].split(":"))[1], cmb);
				}
			}
		}
		
		request.setAttribute("projectId", projectId);
		return mapping.findForward("success");
	}
	private void addModelToUnit(String ids,CateModelBean cm){
		String[] id=ids.split(",");
		for(int i=0;i<id.length;i++){
			if(!id[i].equals("") && id[i]!=null)
				cm.addModelToUnit("Shot",Integer.valueOf(id[i]));
		}
	}
}
